﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace IntoCommon.Util
{
    public sealed class GlobalFunc
    {
        public static T DeepCopy<T>(T obj) where T : class
        {
            object result;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                result = serializer.Deserialize(ms);
                ms.Close();
            }
            return (T)result;
        }
        public static T LoadXmlFile<T>(string xmlPath)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            StreamReader streamReader = null;

            try
            {
                streamReader = new StreamReader(xmlPath);
                return (T)(xmlSerializer.Deserialize(streamReader));
            }
            catch
            {
                return default;
            }
            finally
            {
                if (null != streamReader)
                {
                    streamReader.Close();
                }
            }
        }
        public static bool WriteXmlFile<T>(string filePath, T xmlObject)
        {
            try
            {
                string dir = Directory.GetParent(filePath).FullName;
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                if (!File.Exists(filePath))
                    File.Create(filePath).Close();

                XmlSerializerNamespaces xN = new XmlSerializerNamespaces();
                xN.Add("", "");
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (StreamWriter sw = new StreamWriter(filePath, false))
                {
                    xmlSerializer.Serialize(sw, xmlObject);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CheckIsNumberic(string strText)
        {
            if (string.IsNullOrEmpty(strText))
            {
                return false;
            }
            //利用正则表达式进行检查
            string pattern = @"^\-?\d*$";
            //1.检查是否是整数
            if (Regex.IsMatch(strText, pattern))
            {
                return true;
            }
            else
            {
                //检查是否浮点数
                pattern = @"^-?\d+(\.\d*)$";
                if (Regex.IsMatch(strText, pattern))
                {
                    return true;
                }
            }
            return false;
        }

        public static byte[] ImageToBytes(string imagePath)
        {
            using (Image image = Image.FromFile(imagePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    // 将图片保存到内存流中
                    image.Save(ms, image.RawFormat);
                    // 返回内存流中的字节数组
                    return ms.ToArray();
                }
            }
        }

        public static Image BytesToImage(byte[] streamByte)
        {
            MemoryStream ms = new MemoryStream(streamByte);
            Image img = Image.FromStream(ms);
            return img;
        }


        public static byte[] HexStringToBytes(string hs)//十六进制字符串转byte
        {
            string a = hs.Replace(" ", "");
            int bytelength = 0;
            if (a.Length % 2 == 0)
            {
                bytelength = a.Length / 2;
            }
            else
            {
                bytelength = a.Length / 2 + 1;
            }
            byte[] b = new byte[bytelength];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < bytelength; i++)
            {
                if (i == bytelength - 1)
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2), 16);
                }
                else
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
                }
            }
            //按照指定编码将字节数组变为字符串
            return b;
        }


        //判断程序是否启动，避免重复打开
        public static bool IsProcessRunning(string processName)
        {
            return Process.GetProcessesByName(processName).Length > 0;
        }
        //关闭程序
        public static void CloseProcess(string processName)
        {
            foreach (var process in Process.GetProcessesByName(processName))
            {
                process.Kill();
                process.WaitForExit();
            }
        }
        //启动程序
        public static void StartProcess(string processName)
        {
            Process.Start(processName);
        }

    }
}
